using System;
using System.Collections;
using System.Collections.Generic;
using cfg.Myskill;
using cfg.skill;
using QFramework;
using UnityEngine;
using Random = UnityEngine.Random;

public class CloneController : MonoBehaviour
{
    protected CloneBase skillInfo;
    protected float cloneDuration;
    protected float cloneTimer;
    protected float disapperTimer;
    protected float disapperSpeed;

    protected PlayerStateController controller;
    protected SpriteRenderer sr;
    protected Animator animator;
    protected Rigidbody2D rb;
    protected Collider2D cd;
    protected int faceDir = 1;
    protected virtual void Awake()
    {
        sr = GetComponentInChildren<SpriteRenderer>();
        animator = GetComponentInChildren<Animator>();
        rb = GetComponentInChildren<Rigidbody2D>();
        cd = GetComponentInChildren<Collider2D>();

    }
    
    public virtual void Play(string animName)
    {
        if (animator.enabled)
        {
            animator.Play(animName);
        }
    }
    
    public  virtual void Init(CloneBase _info, Vector2 pos,int faceDir)
    {
        skillInfo = _info;
        cloneDuration = skillInfo.CloneDuration==-1?999999:skillInfo.CloneDuration;// -1表示一直存在
        disapperSpeed = skillInfo.DisappearSpeed;
        cloneTimer = cloneDuration;
        transform.position = pos;
        this.faceDir = faceDir;
        sr.color = new Color(sr.color.r,sr.color.g,sr.color.b,1);
        disapperTimer = 1f;// 消失时间默认一秒后消失
        transform.eulerAngles = new Vector3(0,faceDir==1?0:180,0);
        controller  = PlayerManager.Instance.controller;
    }
    
   protected virtual void Update()
    {
        cloneTimer -= Time.deltaTime;   
        if(cloneTimer <0)
        {
            disapperTimer -= Time.deltaTime;
            sr.color = new Color(sr.color.r,sr.color.g,sr.color.b,sr.color.a - Time.deltaTime*disapperSpeed);
            if(disapperTimer <= 0|| sr.color.a <= 0)
                DestroySelf();
                
        }
    }

    protected  virtual void DestroySelf()
    {
        Reset();
        PoolMgr.Instance.PushObj(gameObject);
    }

    public virtual void TriggerFinish()
    {
        cloneTimer = -1;
    }
    
    public void TriggerAttackCancel()
    {
    }

    public virtual void Reset()
    {
        
    }
}
